blog

Home / DeveloperSection / Blogs / How do I use CTE to simplify complex queries in SQL Server?

How do I use CTE to simplify complex queries in SQL Server?

How do I use CTE to simplify complex queries in SQL Server?

Ravi Vishwakarma 415 03-Jul-2024

Common Table Expressions (CTEs) are a powerful feature in SQL Server that can simplify complex queries by breaking them down into more manageable parts. A CTE is a temporary result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. 

Here's how to use CTEs to simplify complex queries:

Basic Syntax of a CTE

The basic syntax of a CTE is as follows:

WITH CTEName (Column1, Column2, ...)
AS (
    -- CTE definition
    SELECT Column1, Column2, ...
    FROM YourTable
    WHERE Condition
)
-- Using the CTE in a query
SELECT *
FROM CTEName

Let's consider an example where you have the following tables:

Employees Table:

EmployeeID FirstName LastName DepartmentID Salary
1 John Smith 1 50000
2 Jane Doe 2 60000
3 Michael Johnson 1 55000
4 Emily Brown 3 52000

Departments Table:

DepartmentID DepartmentName
1 HR
2 Finance
3 IT

Using a CTE to Simplify a Query

Suppose you want to retrieve the names of employees along with their department names and calculate the average salary per department. You can use a CTE to simplify the query.

-- Define the CTE
WITH EmployeeDetails AS (
    SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName, e.Salary
    FROM Employees e
    JOIN Departments d ON e.DepartmentID = d.DepartmentID
),
AverageSalaries AS (
    SELECT DepartmentName, AVG(Salary) AS AvgSalary
    FROM EmployeeDetails
    GROUP BY DepartmentName
)
-- Using the CTEs in a final query
SELECT ed.FirstName, ed.LastName, ed.DepartmentName, ed.Salary, avs.AvgSalary
FROM EmployeeDetails ed
JOIN AverageSalaries avs ON ed.DepartmentName = avs.DepartmentName
ORDER BY ed.DepartmentName, ed.LastName;

Intermediate CTE Results:

EmployeeDetails CTE:

EmployeeID FirstName LastName DepartmentName Salary
1 John Smith HR 50000
2 Jane Doe Finance 60000
3 Michael Johnson HR 55000
4 Emily Brown IT 52000

AverageSalaries CTE:

DepartmentName AvgSalary
HR 52500
Finance 60000
IT 52000

Final Result:

FirstName LastName DepartmentName Salary AvgSalary
John Smith HR 50000 52500
Michael Johnson HR 55000 52500
Jane Doe Finance 60000 60000
Emily Brown IT 52000 52000

 

Explanation:

EmployeeDetails CTE: This CTE retrieves the basic details of employees along with their department names by joining the Employees and Departments tables.

AverageSalaries CTE: This CTE calculates the average salary for each department using the data from the EmployeeDetails CTE.

Final Query: The final query selects data from the EmployeeDetails CTE and joins it with the AverageSalaries CTE to include the average salary for each department in the result set.

 

Benefits of Using CTEs:

  1. Readability: Breaking down complex queries into smaller, more manageable CTEs makes the overall query easier to read and understand.
  2. Reusability: You can define intermediate result sets once and reference them multiple times within the same query.
  3. Maintenance: Queries are easier to maintain and modify because each part of the query is separated into logical sections.
  4. Modularity: Complex logic can be encapsulated in CTEs, allowing you to build more modular and comprehensible queries.

By using CTEs, you can simplify and streamline complex queries, making them easier to write, read, and maintain in SQL Server.

Difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN in SQL Server.

Why do you use SQL Command and Queries in SQL Server?


Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By